<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 * $Id: 2018-03-02 17:14:35 apple $
 * 微信客服功能接口：https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1458044813
*/
namespace core;
defined('BY_JYA') or exit('error');
class kefu{
	public function __construct($wechat_id=0){
		global $_AR;
		$this->access_token = cfc('account')->getAccessToken();
	}
	public function getkflist(){
		global $_AR;
		$url = "https://api.weixin.qq.com/cgi-bin/customservice/getkflist?access_token=".$this->access_token;
		$data = cfc('http')->ihttp_request($url);
		$content=$data['content'];
		$content=json_decode($content,true);
		if($content['errcode'] != 0){
			exi('错误'.$content['errcode'].'：'.$this->error_code($content['errcode']),'error');
		}
		return $content;
	}
	public function getonlinekflist(){
		global $_AR;
		$url = "https://api.weixin.qq.com/cgi-bin/customservice/getonlinekflist?access_token=".$this->access_token;
		$data = cfc('http')->ihttp_request($url);
		$content=$data['content'];
		$content=json_decode($content,true);
		if($content['errcode'] != 0){
			exi('错误'.$content['errcode'].'：'.$this->error_code($content['errcode']),'error');
		}
		return $content;
	}
	public function setkefu($kf_account,$nickname){
		global $_AR;
		$url = "https://api.weixin.qq.com/customservice/kfaccount/add?access_token=".$this->access_token;
		$post=array('kf_account'=>$kf_account,'nickname'=>$nickname);
		$post=ijson_encode($post);
		$data = cfc('http')->ihttp_request($url,$post);
		$content=$data['content'];
		$content=json_decode($content,true);
		if($content['errcode'] != 0){
			exi('错误'.$content['errcode'].'：'.$this->error_code($content['errcode']),'error');
		}
		return $content;
	}
	public function inviteworker($kf_account,$invite_wx){
		global $_AR;
		$url = "https://api.weixin.qq.com/customservice/kfaccount/inviteworker?access_token=".$this->access_token;
		$post=array('kf_account'=>$kf_account,'invite_wx'=>$invite_wx);
		$post=ijson_encode($post);
		$data = cfc('http')->ihttp_request($url,$post);
		$content=$data['content'];
		$content=json_decode($content,true);
		if($content['errcode'] != 0){
			exi('错误'.$content['errcode'].'：'.$this->error_code($content['errcode']),'error');
		}
		return $content;
	}
	public function upkefuinfo($kf_account,$nickname){
		global $_AR;
		$url = "https://api.weixin.qq.com/customservice/kfaccount/update?access_token=".$this->access_token;
		$post=array('kf_account'=>$kf_account,'nickname'=>$nickname);
		$post=ijson_encode($post);
		$data = cfc('http')->ihttp_request($url,$post);
		$content=$data['content'];
		$content=json_decode($content,true);
		return $content;
	}
	public function uploadheadimg($kf_account,$media){
		global $_AR;
		$url = "https://api.weixin.qq.com/customservice/kfaccount/uploadheadimg?access_token=".$this->access_token."&kf_account=".$kf_account;
		$post=array('kf_account'=>$kf_account,'media'=>$media);
		$post=ijson_encode($post);
		$data = cfc('http')->ihttp_request($url,$post);
		$content=$data['content'];
		$content=json_decode($content,true);
		if($content['errcode'] != 0){
			exi('错误'.$content['errcode'].'：'.$this->error_code($content['errcode']),'error');
		}
		return $content;
	}
	public function delkefu($kf_account){
		global $_AR;
		$url = "https://api.weixin.qq.com/customservice/kfaccount/del?access_token=".$this->access_token."&kf_account=".$kf_account;
		$data = cfc('http')->ihttp_request($url);
		$content=$data['content'];
		$content=json_decode($content,true);
		if($content['errcode'] != 0){
			//exi('错误'.$content['errcode'].'：'.$this->error_code($content['errcode']),'error');
		}
		return $content;
	}
	public function setkfsession($kf_account,$openid){
		global $_AR;
		$url = "https://api.weixin.qq.com/customservice/kfsession/create?access_token=".$this->access_token;
		$post=array('kf_account'=>$kf_account,'openid'=>$openid);
		$post=ijson_encode($post);
		$data = cfc('http')->ihttp_request($url,$post);
		$content=$data['content'];
		$content=json_decode($content,true);
		return $content;
	}
	public function kfclose($openid){
		global $_AR;
		$url = "https://api.weixin.qq.com/customservice/kfsession/getsession?access_token=".$this->access_token."&openid=".$openid;
		$data = cfc('http')->ihttp_request($url);
		$content=$data['content'];
		$content=json_decode($content,true);
		if($content['errcode'] != 0){
			exi('错误'.$content['errcode'].'：'.$this->error_code($content['errcode']),'error');
		}
		return $content;
	}
	public function getwaitcase($kf_account){
		global $_AR;
		$url = "https://api.weixin.qq.com/customservice/kfsession/getwaitcase?access_token=".$this->access_token;
		$data = cfc('http')->ihttp_request($url);
		$content=$data['content'];
		$content=json_decode($content,true);
		if($content['errcode'] != 0){
			exi('错误'.$content['errcode'].'：'.$this->error_code($content['errcode']),'error');
		}
		return $content;
	}
	public function getmsglist($starttime,$endtime,$msgid,$number){
		global $_AR;
		$url = "https://api.weixin.qq.com/customservice/msgrecord/getmsglist?access_token=".$this->access_token;
		$data = cfc('http')->ihttp_request($url);
		$content=$data['content'];
		$content=json_decode($content,true);
		if($content['errcode'] != 0){
			exi('错误'.$content['errcode'].'：'.$this->error_code($content['errcode']),'error');
		}
		return $content;
	}
	
	//错误码
	public function error_code($code, $errmsg = '未知错误') {//获取错误详情
		global $_AR;
		$errors = array(
			'0'	=> '成功',
			'65400'	=> 'API不可用，即没有开通/升级到新版客服功能',
			'65401'	=> '无效客服帐号',
			'65402'	=> '帐号尚未绑定微信号，不能投入使用',
			'65403'	=> '客服昵称不合法',
			'65404'	=> '客服帐号不合法',
			'65405'	=> '帐号数目已达到上限，不能继续添加',
			'65406'	=> '已经存在的客服帐号',
			'65407'	=> '邀请对象已经是该公众号客服',
			'65408'	=> '本公众号已经有一个邀请给该微信',
			'65409'	=> '无效的微信号',
			'65410'	=> '邀请对象绑定公众号客服数达到上限（目前每个微信号可以绑定5个公众号客服帐号）',
			'65411'	=> '该帐号已经有一个等待确认的邀请，不能重复邀请',
			'65412'	=> '该帐号已经绑定微信号，不能进行邀请',
			'65413'	=> '不存在对应用户的会话信息',
			'65414'	=> '粉丝正在被其他客服接待',
			'65415'	=> '指定的客服不在线',
			'65416' => '查询参数不合法',
			'65417' => '查询时间段超出限制',
			'40001'	=> 'access_token获取失败',
			'40003'	=> '非法的openid',
			'40005'	=> '不支持的媒体类型',
			'40009'	=> '媒体文件长度不合法',
		);
		$code = strval($code);
		if($errors[$code]) {
			return $errors[$code];
		} else {
			$errmsg = $errmsg.',错误代码：'.$code;
			return $errmsg;
		}
	}
	
}